/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulatefire.Building.Fire;

import simulatefire.Building.Building;
import simulatefire.Building.RoomParts.RoomParts;

/**
 *
 * @author Nicolas
 * @version $Revision: 1.0 $
 */
public class Fire {
    
    Building building;
    StartPoint sp;
    FireParts fp[];
    
    
    /**
     * Constructor for Fire.
     * @param b Building
     * @throws InterruptedException
     */
    public Fire(Building b) throws InterruptedException{
        
        this.building = b;
        sp = new StartPoint(b);
        building.printBuilding();
        
    }
    
    /**
     * Method start.
     * @throws InterruptedException
     */
    public void start() throws InterruptedException{
        
        boolean finish = false;
        int count1 = 0;
        int count2 = 0;
        RoomParts map[][][] = building.getMap();
        int longueur = building.getLongeur();
        int largeur = building.getLargeur();
        int hauteur = building.getHauteur();
        
        
        for(int i=0; i < longueur; i++){
            for(int j=0; j < largeur; j++){
                for(int k=0; k < hauteur; k++){
                    
                    if(i-1 > -1) if(map[i-1][j][k].getOnFire() > 50) map[i][j][k].setFire(25);
                    if(j-1 > -1) if(map[i][j-1][k].getOnFire() > 50) map[i][j][k].setFire(25);
                    if(i+1 < longueur) if(map[i+1][j][k].getOnFire() > 50) map[i][j][k].setFire(25);
                    if(j+1 < largeur) if(map[i][j+1][k].getOnFire() > 50) map[i][j][k].setFire(25);
                    
                    System.out.println();
                    
                    count1++;
                    if(map[i][j][k].getOnFire() == 100) count2++;
                }
            }
        }
        if (count1 == count2) finish = true;
        
        building.printBuilding();
        if(!finish) this.start();
        else resetFire(longueur, largeur, hauteur, map);
        
    }
    
    /**
     * Method resetFire.
     * @param lo int
     * @param la int
     * @param ha int
     * @param map RoomParts[][][]
     */
    private void resetFire(int lo, int la, int ha, RoomParts[][][] map){
        for(int i=0; i < lo; i++){
            for(int j=0; j < la; j++){
                for(int k=0; k < ha; k++){
                    map[i][j][k].resetFire();
                }
            }
        }
        
        map[sp.getX()][sp.getY()][sp.getZ()].setFire();
    }
    
}
